package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.TCargoArea;
import com.tmeone.erpone.entity.TWare;
import com.tmeone.erpone.service.ITCargoAreaService;
import com.tmeone.erpone.service.ITWareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

import java.util.List;

/**
 * <p>
 *  库区控制器
 * </p>
 *
 * @author wzy
 * @since 2021-04-19
 */
@RestController
public class TCargoAreaController {
    @Autowired
    private ITCargoAreaService itCargoAreaService;
    @Autowired
    private ITCargoAreaService cargoAreaService;
    @Autowired
    private ITWareService wareService;

    @RequestMapping("/selectCargoAreaAllgoodsName")
    public JsonResult selectCargoAreaAllgoodsName(String val){
        List<TCargoArea> list = itCargoAreaService.list(new QueryWrapper<TCargoArea>().lambda().eq(TCargoArea::getWareId,val));
        return new JsonResult(1,list);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("caregoAreas")
    public JsonResult selectAllCaregoAreaList() {
        return new JsonResult(1, cargoAreaService.list());
    }

    /**
     * 通过Id修改货区状态
     * @param cargoArea
     * @return
     */
    @PutMapping("cargoAreaStatus")
    public JsonResult modifyCargoAreaStatus(@RequestBody TCargoArea cargoArea) {
        return new JsonResult(1, cargoAreaService.updateById(cargoArea));
    }

    /**
     * 分页查询
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("pageCargoArea")
    public JsonResult pageCargoAreaStatus(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        Page<TCargoArea> page = new Page<>(current, pageSize);
        IPage<TCargoArea> pageCaregoAreas = cargoAreaService.findPageCaregoAreas(page, null, null);
        return new JsonResult(1, pageCaregoAreas);
    }

    /**
     * 多条件查询
     * @param wareId
     * @param goodsId
     * @return
     */
    @GetMapping("caregoAreas/{wareId}/{goodsId}")
    public JsonResult selectCaregoAreasByCondition(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
            @PathVariable(value = "wareId", required = false) String wareId,
            @PathVariable(value = "goodsId", required = false) String goodsId) {
        if (wareId.equals("1")){
            wareId="";
        }
        if (goodsId.equals("1")){
            goodsId="";
        }
        Page<TCargoArea> page = new Page<>(current, pageSize);
        return new JsonResult(1, cargoAreaService.findPageCaregoAreas(page, wareId, goodsId));
    }

    /**
     * 新增货区
     * @param cargoArea
     * @return
     */
    @PostMapping("cargoArea")
    public JsonResult insertCaregoArea(@RequestBody TCargoArea cargoArea){
        cargoArea.setId(Long.toString(System.currentTimeMillis()));
        cargoArea.setPersonId(wareService.getOne(new QueryWrapper<TWare>().lambda().eq(TWare::getPerson,cargoArea.getPersonId())).getWareNo());
        cargoArea.setWareId(wareService.getOne(new QueryWrapper<TWare>().lambda().eq(TWare::getWareName,cargoArea.getWareId())).getWareNo());
        cargoAreaService.save(cargoArea);
        return new JsonResult(1,null);
    }

    /**
     * 修改货区
     * @param cargoArea
     * @return
     */
    @PutMapping("cargoArea")
    public JsonResult modifyCaregoArea(@RequestBody TCargoArea cargoArea){
        cargoArea.setPersonId(wareService.getOne(new QueryWrapper<TWare>().lambda().eq(TWare::getPerson,cargoArea.getPersonId())).getWareNo());
        cargoArea.setWareId(wareService.getOne(new QueryWrapper<TWare>().lambda().eq(TWare::getWareName,cargoArea.getWareId())).getWareNo());
        cargoAreaService.updateById(cargoArea);
        return new JsonResult(1,null);
    }
}
